package com.etl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etl.vo.EtlStatisticsTableUseTimeVO;
import com.etl.vo.EtlStatisticsVO;
import com.google.common.collect.Lists;
import com.etl.entity.EtlBusinessBatch;
import com.etl.entity.EtlTaskMain;
import com.etl.mapper.EtlBusinessBatchMapper;
import com.etl.mapper.EtlTaskLogMapper;
import com.etl.mapper.EtlTaskMainMapper;
import com.etl.service.IEtlStatisticsService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by Administrator on 2019/10/21.
 */
@Service
@Slf4j
public class EtlStatisticsServiceImpl implements IEtlStatisticsService {

    @Resource
    private EtlTaskMainMapper etlTaskMainMapper;

    @Resource
    private EtlBusinessBatchMapper businessBatchMapper;

    @Resource
    private EtlTaskLogMapper etlTaskLogMapper;

    @Override
    public List<EtlStatisticsVO> mainStatistics() {
        QueryWrapper<EtlBusinessBatch> etlBusinessBatchQueryWrapper = new QueryWrapper<>();
        etlBusinessBatchQueryWrapper.orderByAsc("batch");
        List<EtlBusinessBatch> batchList = businessBatchMapper.selectList(etlBusinessBatchQueryWrapper);
        //查出所有任务
        QueryWrapper<EtlTaskMain> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("batch");
        List<EtlTaskMain> etlTaskMainList = etlTaskMainMapper.selectList(queryWrapper);
        List<EtlStatisticsVO> etlStatisticsVOS = Lists.newArrayList();
        for (EtlBusinessBatch batchItem : batchList) {
            EtlStatisticsVO etlStatisticsVO = new EtlStatisticsVO();
            etlStatisticsVO.setBatch(batchItem.getBatch());
            etlStatisticsVO.setName(batchItem.getBatchName());
            etlStatisticsVOS.add(etlStatisticsVO);
        }
        for (EtlStatisticsVO etlStatisticsVO : etlStatisticsVOS) {
            for (EtlTaskMain item : etlTaskMainList) {
                if (etlStatisticsVO.getBatch().equals(item.getBatch())) {
                    /**
                     * 开启状态下的抽取开始时间与结束时间
                     */
                    if (item.getFlag().intValue() == 1) {
                        if (item.getBeginTime() != null && etlStatisticsVO.getBeginTime() == null) {
                            etlStatisticsVO.setBeginTime(item.getBeginTime());     //批次任务开始抽取时间
                        } else if (item.getBeginTime() != null && etlStatisticsVO.getBeginTime() != null && item.getBeginTime().compareTo(etlStatisticsVO.getBeginTime()) < 0) {
                            etlStatisticsVO.setBeginTime(item.getBeginTime());
                        }

                        if (item.getLastTime() != null && etlStatisticsVO.getEndTime() == null) {
                            etlStatisticsVO.setEndTime(item.getLastTime());  //批次任务结束抽取时间
                        } else if (item.getLastTime() != null && etlStatisticsVO.getEndTime() != null && item.getLastTime().compareTo(etlStatisticsVO.getEndTime()) > 0) {
                            etlStatisticsVO.setEndTime(item.getLastTime());
                        }
                    }
                    /**
                     * 任务开启数量和关闭数量
                     */
                    if (item.getFlag().intValue() == 0) {
                        int closeCount = etlStatisticsVO.getCloseCount();
                        closeCount++;
                        etlStatisticsVO.setCloseCount(closeCount);
                    } else {
                        int startCount = etlStatisticsVO.getStartCount();
                        startCount++;
                        etlStatisticsVO.setStartCount(startCount);
                    }
                    /**
                     * 任务不同状态数量
                     */
                    if (item.getExecFlag().intValue() == 0) {
                        int waitCount = etlStatisticsVO.getWaitCount();
                        waitCount++;
                        etlStatisticsVO.setWaitCount(waitCount);
                    } else if (item.getExecFlag().intValue() == 1) {
                        int runningCount = etlStatisticsVO.getRunningCount();
                        runningCount++;
                        etlStatisticsVO.setRunningCount(runningCount);
                    } else if (item.getExecFlag().intValue() == 2) {
                        int failedCount = etlStatisticsVO.getFailedCount();
                        failedCount++;
                        etlStatisticsVO.setFailedCount(failedCount);
                    } else {
                        int successCount = etlStatisticsVO.getSuccessCount();
                        successCount++;
                        etlStatisticsVO.setSuccessCount(successCount);
                    }
                }
            }
        }
        return etlStatisticsVOS;
    }


    @Override
    public List<EtlStatisticsTableUseTimeVO> taskRunningTime(Integer batch) {
        List<EtlStatisticsTableUseTimeVO> etlStatisticsTableUseTimeVOS = etlTaskLogMapper.findTableRunningTimeList(batch);
        return etlStatisticsTableUseTimeVOS;
    }
}

